package com.one.group.admin.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mybatisflex.core.query.QueryWrapper;
import com.one.group.enums.ErrorCode;
import com.one.group.model.dto.BaseDeleteDto;
import com.one.group.model.dto.MaintenanceRecordAddDto;
import com.one.group.model.dto.MaintenanceRecordQueryDto;
import com.one.group.model.dto.MaintenanceRecordUpdateDto;
import com.one.group.model.entity.MaintenanceRecord;
import com.one.group.model.vo.MaintenanceRecordVo;
import com.one.group.result.Result;
import com.one.group.service.MaintenanceRecordService;
import com.one.group.utils.ResultUtil;
import com.one.group.utils.ThrowUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * {@code @Create:}  2025-09-28  14 : 30
 * {@code @Author:} Cherry
 * {@code @ToUser:} 凡是发生,皆有利于我。
 * ------------------------------------
 * {@code @note:}
 */

@SuppressWarnings({"all"})
@RestController
@RequestMapping("/maintenanceRecord")
@Slf4j
@Tag(name = "保养记录接口")
public class MaintenanceRecordController {
    @Resource
    private MaintenanceRecordService maintenanceRecordService;


    @GetMapping("/info")
    @Operation(summary = "保养记录详情接口")
    public Result getInfo(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        QueryWrapper wrapper = new QueryWrapper().eq(MaintenanceRecord::getId, id);
        MaintenanceRecord maintenanceRecord = maintenanceRecordService.getOne(wrapper);
        return maintenanceRecord != null ? ResultUtil.success(maintenanceRecord) : ResultUtil.error(ErrorCode.NOT_FOUND_ERROR);
    }


    @PostMapping("/list")
    @Operation(summary = "保养记录列表接口")
    public Result list(@RequestBody MaintenanceRecordQueryDto maintenanceRecordQueryDto) {

        new PageHelper().startPage(maintenanceRecordQueryDto.getPage(), maintenanceRecordQueryDto.getLimit());

        List<MaintenanceRecordVo> maintenanceRecordVoList = maintenanceRecordService.listSpMaintenanceRecords(maintenanceRecordQueryDto);

        PageInfo<MaintenanceRecordVo> maintenanceRecordVoPageInfo = new PageInfo<>(maintenanceRecordVoList);

        return ResultUtil.success(maintenanceRecordVoPageInfo);
    }

    @PostMapping("/add")
    @Operation(summary = "保养记录添加接口")
    public Result add(@RequestBody MaintenanceRecordAddDto maintenanceRecordAddDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(maintenanceRecordAddDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenanceRecordService.addSpMaintenanceRecord(maintenanceRecordAddDto);
        return ResultUtil.success("添加成功");
    }

    @PutMapping("/update")
    @Operation(summary = "保养记录修改接口")
    public Result update(@RequestBody MaintenanceRecordUpdateDto maintenanceRecordUpdateDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(maintenanceRecordUpdateDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenanceRecordService.updateSpMaintenanceRecord(maintenanceRecordUpdateDto);
        return ResultUtil.success("修改成功");
    }

    @DeleteMapping("/delete")
    @Operation(summary = "保养记录删除接口")
    public Result delete(@RequestParam Long id) {
        ThrowUtil.throwIf(id == null, ErrorCode.PARAMS_ERROR, "参数不能为空");
        boolean result = maintenanceRecordService.removeById(id);
        return result ? ResultUtil.success("删除成功") : ResultUtil.error(ErrorCode.OPERATION_ERROR);
    }

    @DeleteMapping("/deleteByBatch")
    @Operation(summary = "保养记录批量删除接口")
    public Result delete(@RequestBody BaseDeleteDto baseDeleteDto) {
        ThrowUtil.throwIf(BeanUtil.isEmpty(baseDeleteDto), ErrorCode.PARAMS_ERROR, "参数不能为空");
        maintenanceRecordService.batchDeleteSpMaintenanceRecords(baseDeleteDto.getIds());
        return ResultUtil.success("删除成功");
    }

}
